﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net.Mime;
using System.Text;
using Nivot.PowerShell.WindowsMobile.Rapi2;

namespace Nivot.PowerShell.WindowsMobile
{
    public class CEImageFile : CEMediaFile
    {
        internal CEImageFile(RemoteDevice device, string path)
            : base(device, path)
        {
        }

        public static implicit operator Image(CEImageFile file)
        {
            string tempFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(file.FullPath));

            Image image = null;

            try
            {
                RemoteDevice device = (new RemoteDeviceManager().Devices.FirstConnectedDevice);
                device.CopyFileFromDevice(file.FullPath, tempFile, true);
                image = Image.FromFile(tempFile);
            }
            catch (RapiException ex)
            {
                throw;
            }
            catch (IOException ex)
            {
                throw;
            }
            return image;
        }

        public override CEInvokable Invokable
        {
            get
            {
                return CEInvokable.Remotely | CEInvokable.Locally;
            }
        }
    }
}
